import { HttpStatus } from '@nestjs/common';
import { PaginationType, ResponseBodyBaseType } from 'src/typings';
import { ResponsePaginationType } from './types';

/**
 * 基本成功返回
 * @param config 需要更改的默认参数
 * @returns
 */
export const success = (config?: {
  message?: string;
}): ResponseBodyBaseType => {
  const { message } = {
    message: '成功',
    ...config,
  };
  return {
    statusCode: HttpStatus.OK,
    success: true,
    message
  };
};

/**
 * 查询成功返回
 * @param config 需要更改的默认参数
 * @returns
 */
export const successPagination = <T>(config?: {
  pagination: PaginationType;
  data: T;
  message?: string;
}) => {
  const { message, data, pagination } = {
    message: '列表获取成功',
    ...config,
  };
  return {
    statusCode: HttpStatus.OK,
    success: true,
    message,
    data,
    ...pagination,
  };
};

/**
 * 查询所有成功返回
 * @param config 需要更改的默认参数
 * @returns
 */
export const successFindAll = <T>(config?: {
  data: T;
  total: number;
  message?: string;
}): ResponsePaginationType => {
  const { message, data, total } = {
    message: '列表获取成功',
    ...config,
  };
  return {
    statusCode: HttpStatus.OK,
    success: true,
    message,
    data,
    total
  };
};

/**
 * 查询一个成功返回
 * @param config 需要更改的默认参数
 * @returns
 */
export const successFindOne = <T>(config?: {
  data: T;
  message?: string;
}) => {
  const { message, data } = {
    message: '数据获取成功',
    ...config,
  };
  return {
    statusCode: HttpStatus.OK,
    success: true,
    message,
    data
  };
};

/**
 * 请求成功有数据返回
 * @param config 需要更改的默认参数
 * @returns
 */
export const successData = <T>(config?: {
  data: T;
  message?: string;
}): ResponsePaginationType => {
  const { message, data } = {
    message: '请求成功',
    ...config,
  };
  return {
    statusCode: HttpStatus.OK,
    success: true,
    message,
    data
  };
};

/**
 * 删除成功返回
 * @param config 需要更改的默认参数
 * @returns
 */
export const successDelete = (config?: {
  message?: string;
}) => {
  const { message } = {
    message: '删除成功',
    ...config,
  };
  return {
    statusCode: HttpStatus.OK,
    success: true,
    message,
  };
};

/**
 * 添加成功返回
 * @param config 需要更改的默认参数
 * @returns
 */
export const successAdd = (config?: { message?: string }): ResponseBodyBaseType => {
  const { message } = {
    message: '添加成功',
    ...config,
  };
  return {
    statusCode: HttpStatus.OK,
    success: true,
    message,
  };
};

/**
 * 更新成功返回
 * @param config 需要更改的默认参数
 * @returns
 */
export const successUpdate = (config?: {
  message?: string;
}): ResponseBodyBaseType => {
  const { message } = {
    message: '更新成功',
    ...config,
  };
  return {
    statusCode: HttpStatus.OK,
    success: true,
    message,
  };
};
